package torch

import (
	"github.com/astaxie/beego"
	"path"
	"time"
)

func All(s ...string) bool {
	flag := true
	for _, val := range s {
		if val == "" {
			flag = false
			return flag
		}
	}

	return flag

}

func UploadFile(this *beego.Controller, filePath string, temp string) string {
	//处理文件上传
	file, head, err := this.GetFile(filePath)
	if head.Filename == "" {
		return "NoImg"
	}

	if err != nil {
		this.Data["errmsg"] = "文件上传失败"
		this.TplName = temp
		return ""
	}
	defer file.Close()

	//1.文件大小
	if head.Size > 5000000 {
		this.Data["errmsg"] = "文件太大，请重新上传"
		this.TplName = temp
		return ""
	}

	//2.文件格式
	//a.jpg
	ext := path.Ext(head.Filename)
	if ext != ".jpg" && ext != ".png" && ext != ".jpeg" {
		this.Data["errmsg"] = "文件格式错误。请重新上传"
		this.TplName = temp
		return ""
	}

	fileName := time.Now().String() + path.Ext(head.Filename)

	//存储
	this.SaveToFile("./static/img/"+filePath, fileName)
	return "/static/img/" + fileName
}

func PageTool(pageIndex int, pageCount int) []int {
	pages := make([]int, pageCount)
	if pageCount <= 5 {
		for i, _ := range pages {
			pages[i] = i + 1
		}
	} else if pageIndex <= 3 {
		pages = []int{1, 2, 3, 4, 5}
	} else if pageIndex > pageCount-3 {
		pages = []int{pageCount - 4, pageCount - 3, pageCount - 2, pageCount - 1, pageCount}
	} else {
		pages = []int{pageIndex - 2, pageIndex - 1, pageIndex, pageIndex + 1, pageIndex + 2}
	}

	return pages

}
